package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.resp.R;
import com.tcm.service.CommentReportService;
import com.tcm.service.CommentService;
import com.tcm.utils.AccessTokenUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Guqier
 * @version 1.0
 * @date 2023/10/9 20:52
 */
@RestController
@RequestMapping("/pc/comment")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;

    private final CommentReportService commentReportService;

    /**
     * 分页获取评论区列表
     * @param page     当前页号
     * @param pageSize 页面大小
     * @param content  检索评论内容
     * @param account  检索账号
     * @param itemIds  检索题目ID
     * @return 评论区列表
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getCommentListByPage(@RequestParam Integer page,
                                  @RequestParam Integer pageSize,
                                  @RequestParam(required = false) String content,
                                  @RequestParam(required = false) String account,
                                  @RequestParam(required = false) List<Long> itemIds) {
        return R.success(commentService.getCommentListByPage(page, pageSize, content, account, itemIds));
    }

    /**
     * 删除评论
     * @param id 评论ID
     * @return 删除结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeComment(@RequestParam Long id) {
        return commentService.removeComment(id) ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 获取评论举报列表
     * @return 评论举报列表
     */
    @RequestMapping(value = "/report", method = RequestMethod.GET)
    public R getCommentReportList() {
        return R.success(commentReportService.getCommentReportList());
    }

    /**
     * 处理评论举报
     * @param reportId 举报ID
     * @param reason      处理信息（留言）（原因）
     * @param request  请求对象
     * @return 处理结果
     */
    @RequestMapping(value = "/report", method = RequestMethod.DELETE)
    public R handleCommentReport(@RequestParam Long reportId, @RequestParam String reason, @RequestParam Integer type, HttpServletRequest request) {
        if (!StringUtils.hasText(reason)) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        if (!type.equals(1) && !type.equals(2)){
            return R.fail(ResponseEnum.PARAM_IS_INVAlID);
        }
        return commentReportService.handleCommentReport(reportId, reason, AccessTokenUtil.getUserId(request), AccessTokenUtil.getUserAccount(request), type) ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

}
